import React from 'react'
import { http } from '../../../lib/core/http';
import BraftEditor from 'braft-editor';
import apis from '@/apis/AnnouncementManage';
import UpdateComponent from './component/UpdateModal';

const UpdateBtnAuth = 'customer_dealer_notice_manager_update'

class Edit extends UpdateComponent {

  protected handleBtnAuth = UpdateBtnAuth
  constructor(props: any) {
    super(props)
    this.state = {
      //标题
      title: "编辑公告",
      sendToval: []
    }
  }

  componentDidMount() {
    super.componentDidMount()
    this.getDetail()
  }

  public getDetail = () => {

    this.setState({ querying: true })
    http.get(apis.getAnnouncementManagementDetails, { params: { ...this.props.match.params } }).then(res => {
      const detail = res.data || {}
      const enclosure = detail.enclosure
      let annexFile = []
      if (enclosure && enclosure.url) { // 处理附件字段格式{url,name}
        const { url, enclosureName: name } = enclosure
        const fileData = {
          uid: url,
          name,
          url,
          status: 'done',
        }
        annexFile = [fileData]
      }
      if (detail.type === 1) {
        this.setState({ sendToval: detail.userIds })
        // 处理产品类别为数组
        detail.productsType = detail.productsType?.split(',')
        // 获取产品类别对象
        this.getAllProductTypeList()
      } else {
        this.getAllLinkByBrandCode(detail.brandCode)
      }
      // 将data-src处理回src
      detail.text = detail.text?.replace(/data-src/g, 'src')
      detail.userIds = detail.userIds?.map(({ agentCode: value, agentName: label }) => ({ value, label })) || []
      this.setState({
        title: detail.title,
        editorState: BraftEditor.createEditorState(detail.text),
        annexFile,
        detailData: detail,
        sendType: detail.type,
      })
    }).finally(() => this.setState({ querying: false }))
  }
}

export default Edit
